function hide(object){
	if(object.opacity>0){
		clearTimeout(object.st);
		object.opacity-=0.1;
		object.style.opacity=object.opacity;
		object.ht=setTimeout(function(){
			hide(object);
		},50);	
	}else{
		object.style.visibility="hidden";
	}
};

function show(object){
	object.style.visibility="visible";
	if(object.opacity<1){
		clearTimeout(object.ht);
		object.opacity+=0.1;
		object.style.opacity=object.opacity;
		object.st=setTimeout(function(){
			show(object);
		},50);
	}
};

HTMLButtonElement.prototype.ht=-1;
HTMLButtonElement.prototype.st=-1;
HTMLButtonElement.prototype.opacity=0;
HTMLDivElement.prototype.ht=-1;
HTMLDivElement.prototype.st=-1;
HTMLDivElement.prototype.opacity=0;


var editFlag=true;
function showEditButton(o) {
	if(editFlag){
		show(o.childNodes[3]);
	}
}
function hideEditButton(o) {
	hide(o.childNodes[3]);
}



function closeEditMenu(editMenu) {
	editFlag=true;
	hide(editMenu);
}

function showEditMenu(o, id, userId) {
	editFlag=false;
	var p=findPosition(o);
	var editMenu=document.getElementById("editMenu");
	editMenu.style.left=p[0]+"px";
	editMenu.style.top=p[1]+"px";
	show(editMenu);
	var taskNameLink=document.getElementById("name_link_"+id);
	document.getElementById("editName").value=taskNameLink.innerHTML;
	document.getElementById("editLink").value=taskNameLink;
	document.getElementById("editProjectId").value=document.getElementById("projectId_"+id).innerHTML;
	document.getElementById("editID").value=id;
	var select=document.getElementById("editSelect");
	for(var i=0; i<select.options.length; i++){
		if(select.options[i].value==userId){
			if(select.selectedIndex!=i){
				select.selectedIndex=i;
			}		
		}
	}
}

function findPosition(o){
	var left=0,top=0;
	if(o.offsetParent){
		left=o.offsetLeft;
		top=o.offsetTop;
		while(o=o.offsetParent){
			left+=o.offsetLeft;
			top+=o.offsetTop;
		}
	}
	return new Array(left, top);
}

//ajax
var lastImg;
function changeStatus(o, step, id){
	var request=new XMLHttpRequest();
	request.onreadystatechange=function(){
		if(request.readyState==4){
			if(request.status==200){
				if(request.responseText=="no_rights") return;
				var img=o.childNodes[1];
				if(request.responseText!=("no")&&request.responseText!=("yes")){
					var p=findPosition(o);
					var editIssue=document.getElementById("editIssue");
					editIssue.style.left=(p[0]-350)+"px";
					editIssue.style.top=p[1]+"px";
					lastImg=img;
					document.getElementById("issueContent").value=request.responseText;
					editIssue.dataId=id;
					editIssue.dataName=step;
					show(editIssue);
				}else{
					img.src="css/"+request.responseText+".png";
				}
			}
		}
	};
	request.open("GET","AJAXBoardHandler?reason=UPDATE_TASK_STATUS&name="+step+"&id="+id,true);
	request.send();
}


function updateTaskData(editMenu){
	closeEditMenu(editMenu);
	var id=document.getElementById("editID").value;
	var projectId=document.getElementById("editProjectId").value;
	var userId=document.getElementById("editSelect").options[document.getElementById("editSelect").selectedIndex];
	var link=document.getElementById("editLink").value;
	var name=document.getElementById("editName").value;
	var request=new XMLHttpRequest();
	request.onreadystatechange=function(){
		if(request.readyState==4){
			if(request.responseText=="success"){
				document.getElementById("projectId_"+id).innerHTML=projectId;
				document.getElementById("userID_"+id).innerHTML=userId.innerHTML;
				document.getElementById("name_link_"+id).href=link;
				document.getElementById("name_link_"+id).innerHTML=name;
			}else{
				alert("internal server error");
			}
		}
	};
	request.open("GET","AJAXBoardHandler?reason=UPDATE_TASK&id="+id+"&name="+name+"&link="+link+"&account_id="+
			userId.value+"&project_id="+projectId,true);
	request.send(); 
}

function deleteTaskData(editMenu){
	var request=new XMLHttpRequest();
	request.onreadystatechange=function(){
		if(request.readyState==4){
			if(request.responseText=="error"){
				alert("internal server error");
			}else{
				alert("deleted");
				document.location.reload(true);
			}
		}
	};
	request.open("GET","AJAXBoardHandler?reason=DELETE_TASK&id="+document.getElementById("editID").value,true);
	request.send();
	closeEditMenu(editMenu);
}


function showIssueEditor(){
	document.getElementById("issueEditor").style.display = 'block';
	document.getElementById("editMenu").style.height="330px";
}

function hideIssueEditor(){
	document.getElementById("issueEditor").style.display = 'none';
	document.getElementById("editMenu").style.height="170px";
}

function hideEditIssue(){
	document.getElementById("editIssue").style.display = 'none';
}
function hideSingleAdd(){
	document.getElementById("singleAdd").style.display = 'none';
}
function showSingleAdd(o){
	var p=findPosition(o);
	var singleAdd=document.getElementById("singleAdd");
	singleAdd.style.left=(p[0]-350)+"px";
	singleAdd.style.top=p[1]+"px";
	show(singleAdd);
}
function singleTaskAdd(){
	var request=new XMLHttpRequest();
	request.onreadystatechange=function(){
		if(request.readyState==4){
			if(!(request.status==200)){
				alert("database error");
			}else{
				document.location.reload(false);
			}
		}
	};
	request.open("GET","AJAXBoardHandler?reason=ADD_TASK&projectId="+document.getElementById("singleAddProjectId").value+
			"&team="+document.getElementById("singleAddTeam").value+"&name="+document.getElementById("singleAddName").value+
			"&link="+document.getElementById("singleAddLink").value,true);
	request.send(); 
	hideSingleAdd();
}
function updateIssue(){
	var request=new XMLHttpRequest();
	request.onreadystatechange=function(){
		if(request.readyState==4){
			if(!(request.status==200)){
				alert("database error");
			}else{
				document.location.reload(true);
			}
		}
	};
	request.open("GET","AJAXBoardHandler?reason=CREATE_ISSUE&id="+document.getElementById("editID").value+"&issueContent="+document.getElementById("issueText").value,true);
	request.send();
	hideIssueEditor();
}
function resolveIssue(editIssue){
	var request=new XMLHttpRequest();
	request.onreadystatechange=function(){
		if(request.readyState==4){
			if(request.responseText=="error"){
				alert("internal server error");
			}else{
				lastImg.src="css/"+request.responseText+".png";
			}
		}
	};
	request.open("GET","AJAXBoardHandler?reason=RESOLVE_ISSUE&id="+editIssue.dataId+"&name="+editIssue.dataName,true);
	request.send(); 
	closeEditMenu(editIssue);
}
